package cn.liukuan.jps.plugins;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by liukuan on 16/6/3.
 */
public class IKSynonymsCantiner {

    private static Map<String, Set<String>> synonymsMap = new ConcurrentHashMap<String, Set<String>>();

    private IKSynonymsCantiner() {

        System.out.println("load synonyms.dic...");

        InputStream resourceAsStream = IKSynonymsCantiner.class.getClassLoader().getResourceAsStream(IKSameConstants.SYNONYMS_PATH);

        List<String> strings = null;
        try {
            strings = IOUtils.readLines(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (String string : strings) {

            if(string != null && !string.trim().equals("")){

                String[] split = string.split(",");

                if(split.length == 2){

                    Set<String> value = synonymsMap.get(split[0]);

                    if(value == null){
                        HashSet<String> set = new HashSet<String>();
                        set.add(split[1]);
                        synonymsMap.put(split[0], set);
                    }else {
                        Set<String> set = synonymsMap.get(split[0]);
                        set.add(split[1]);
                        synonymsMap.put(split[0], set);
                    }
                }

            }
        }

        System.out.println("synonyms.dic is loaded...");
    }

    public Map getSynonysMap(){
        return synonymsMap;
    }

    private static class IKSynonymsCantinerHolder {
        public static IKSynonymsCantiner Instance = new IKSynonymsCantiner();
    }

    public static IKSynonymsCantiner getInstance(){
        return IKSynonymsCantinerHolder.Instance;
    }
}
